package com.example.costumview.customview2;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;

public class SlideRecyclerView extends RecyclerView {
    //流畅滑动
    private Scroller mScroller;
    //当前选中的item
    private ViewGroup mItem;
    //上次按下的坐标
    private float mLastX = 0f;

    public SlideRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mScroller = new Scroller(context);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        switch (e.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //获取点击的位置
                getSelectItem(e);
                //设置点击的横坐标
                mLastX = e.getX();
                break;
            case MotionEvent.ACTION_MOVE:

                return moveItem(e);
        }
        return super.onInterceptTouchEvent(e);
    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        switch (e.getAction()) {

            case MotionEvent.ACTION_MOVE:
                moveItem(e);
                mLastX = e.getX();
                break;
            case MotionEvent.ACTION_UP:

                stopMove(e);
                break;
        }


        return super.onTouchEvent(e);
    }

    //滑动结束
    //版本一：判断一下结束的位置，补充或恢复位置
    private void stopMove(MotionEvent e) {
        float dx = e.getX() - mLastX;
        //如果移动过半了，应该判定左滑成功
        int deleteWidth = mItem.getChildAt(mItem.getChildCount() - 1).getWidth();
        if (Math.abs(dx) >= deleteWidth / 2f) {

            mScroller.startScroll(mItem.getScrollX(), 0, deleteWidth-mItem.getScrollX(), 0);
            invalidate();
        } else {
            //如果移动没过半应该恢复状态
            mScroller.startScroll(mItem.getScrollX(), 0, -mItem.getScrollX(), 0);
            invalidate();
        }
        //清除状态
        mLastX = 0f;

    }

    /**
     * 移动item
     * 版本一：绝对值小于删除按钮长度随便移动，大于则不移动
     */
    private boolean moveItem(MotionEvent e) {
        float dx = mLastX - e.getX();
        int deleteWidth = mItem.getChildAt(mItem.getChildCount() - 1).getWidth();
        if ((mItem.getScrollX() + dx) <= deleteWidth && (mItem.getScrollX() + dx) >= 0) {
            mItem.scrollBy((int) dx, 0);
            return true;
        }
        return false;
    }

    /**
     * 获取点击位置
     * 版本一：通过点击的y坐标除于item高度得出
     */
    private void getSelectItem(MotionEvent e) {
        Rect frame = new Rect();
        for (int i = 0; i < getChildCount(); i++) {
            View childView = getChildAt(i);
            if (childView.getVisibility() != GONE) {
                childView.getHitRect(frame);
                if (frame.contains((int) e.getX(), (int) e.getY())) {
                    mItem = (ViewGroup) childView;
                }
            }
        }
    }

    @Override
    public void computeScroll() {
        if (mScroller.computeScrollOffset()) {
            mItem.scrollBy(mScroller.getCurrX(), mScroller.getCurrY());
            postInvalidate();
        }
    }
}
